<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${basePackage}.mapper.${mouduleName}.${className}Mapper">
    
    <sql id="column_all">
        <#list columns as column>
        `${column.column_name}` AS '${xcode.getVariableName("${column.column_name}")}'<#if column_index+1 != columns?size>,</#if>
        </#list>
    </sql>

    
    <select id="query" parameterType="long" resultType="${basePackage}.model.${mouduleName}.${className}">
        SELECT 
        <include refid="column_all"/>
        FROM ${schemaName}`${tableName}` WHERE id = ${r"#"}{id} LIMIT 1
    </select>

    
    <select id="lockRow" parameterType="long" resultType="${basePackage}.model.${mouduleName}.${className}">
        SELECT * FROM ${schemaName}`${tableName}` WHERE id = ${r"#"}{id} FOR UPDATE
    </select>

    
    <select id="queryList" parameterType="map" resultType="${basePackage}.model.${mouduleName}.${className}">
        SELECT 
        <include refid="column_all"/>
        FROM ${schemaName}`${tableName}`
        WHERE 1=1
        
        <#--不再显示全部条件
        <#list columns as column>
        <if test="${xcode.getVariableName("${column.column_name}")} != null and ${xcode.getVariableName("${column.column_name}")} != ''">
            AND `${column.column_name}` = ${r"#"}{${xcode.getVariableName("${column.column_name}")}}
        </if>
        </#list>
        -->
        <if test="fieldName != null and fieldName != ''">
            AND `field_name` = ${r"#"}{fieldName}
        </if>
        
        <if test="__SORT != null and __SORT != ''">
            ORDER BY ${r"$"}{__SORT}
        </if>

        <if test="__ORDER != null and __ORDER != ''">
            ${r"$"}{__ORDER}
        </if>

        <if test="__LIMIT != null and __LIMIT != ''">
            LIMIT ${r"$"}{__LIMIT}
        </if>

    </select>

    <select id="queryAll" resultType="${basePackage}.model.${mouduleName}.${className}">
        SELECT
        <include refid="column_all"/>
        FROM ${schemaName}`${tableName}`
    </select>


    <select id="queryPage" parameterType="org.spring.springboot.core.vo.Page" resultType="${basePackage}.model.${mouduleName}.${className}">
        SELECT
        <include refid="column_all"/>
        FROM ${schemaName}`${tableName}`
        WHERE 1=1

        <#--不再显示全部条件
        <#list columns as column>
        <if test="condition.${xcode.getVariableName("${column.column_name}")} != null and condition.${xcode.getVariableName("${column.column_name}")} != ''">
            AND `${column.column_name}` = ${r"#"}{condition.${xcode.getVariableName("${column.column_name}")}}
        </if>
        </#list>
        -->
        <if test="condition.fieldName != null and condition.fieldName != ''">
            AND `field_name` = ${r"#"}{condition.fieldName}
        </if>

        <if test="sort!= null and sort != ''">
            ORDER BY ${r"$"}{sort}
        </if>

        <if test="sort!= null and sort != '' and order!= null and order != ''">
            ${r"$"}{order}
        </if>

    </select>

    <#--获取最后一个字段名称-->
    <#assign last_field="" />
    <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "update_date">
            <#assign last_field=column.column_name />
        </#if>
    </#list>

    <insert id="insert" parameterType="${basePackage}.model.${mouduleName}.${className}" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ${schemaName}`${tableName}`
        (
        <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "update_date">
            `${column.column_name}`<#if last_field != column.column_name>,</#if>
        </#if>
        </#list>
        )
        VALUES
        (
        <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "update_date">
            <#if column.column_name == "create_date">CURRENT_TIMESTAMP()<#else>${r"#"}{${xcode.getVariableName("${column.column_name}")}}</#if><#if last_field != column.column_name>,</#if>
        </#if>
        </#list>
        )
    </insert>


    <insert id="insertList" parameterType="list">
        INSERT INTO ${schemaName}`${tableName}`
        (
        <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "update_date">
            `${column.column_name}`<#if last_field != column.column_name>,</#if>
        </#if>
        </#list>
        )
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
        (
        <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "update_date">
            <#if column.column_name == "create_date">CURRENT_TIMESTAMP()<#else>${r"#"}{item.${xcode.getVariableName("${column.column_name}")}}</#if><#if last_field != column.column_name>,</#if>
        </#if>
        </#list>
        )
        </foreach>
    </insert>


    <#--获取最后一个字段名称-->
    <#assign last_field_="" />
    <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "create_date">
            <#assign last_field_=column.column_name />
        </#if>
    </#list>
    <update id="update" parameterType="${basePackage}.model.${mouduleName}.${className}">
        UPDATE ${schemaName}`${tableName}` SET
        <#list columns as column>
        <#if column.column_name != "id" && column.column_name != "create_date">
        `${column.column_name}` = <#if column.column_name == "update_date">CURRENT_TIMESTAMP()<#else>${r"#"}{${xcode.getVariableName("${column.column_name}")}}</#if><#if last_field_ != column.column_name>,</#if>
        </#if>
        </#list>
        WHERE id = ${r"#"}{id}
    </update>


    <update id="updateDynamic" parameterType="${basePackage}.model.${mouduleName}.${className}">
        UPDATE ${schemaName}`${tableName}`
        <set>
        <#list columns as column>
            <#if column.column_name != "id" && column.column_name != "create_date" && column.column_name != "update_date">
            <#--<#if xcode.isNumber("${column.data_type}")><if test="${xcode.getVariableName("${column.column_name}")} != null"><#else><if test="${xcode.getVariableName("${column.column_name}")} != null and ${xcode.getVariableName("${column.column_name}")} != ''"></#if>-->
            <if test="${xcode.getVariableName("${column.column_name}")} != null">
                `${column.column_name}` = ${r"#"}{${xcode.getVariableName("${column.column_name}")}},
            </if>
            </#if>
            <#if column.column_name == "update_date">
                `update_date` = CURRENT_TIMESTAMP(),
            </#if>
        </#list>
        </set>
        WHERE id = ${r"#"}{id}
    </update>

    
    <delete id="delete" parameterType="long">
        DELETE FROM ${schemaName}`${tableName}` WHERE id = ${r"#"}{id}
    </delete>

    
    <delete id="deleteBy" parameterType="map">
        DELETE FROM ${schemaName}`${tableName}` 
        WHERE 1=1
        
        <#--不再显示全部条件
        <#list columns as column>
        <if test="${xcode.getVariableName("${column.column_name}")} != null and ${xcode.getVariableName("${column.column_name}")} != ''">
            AND `${column.column_name}` = ${r"#"}{${xcode.getVariableName("${column.column_name}")}}
        </if>
        </#list>
        -->
        <if test="fieldName != null and fieldName != ''">
            AND `field_name` = ${r"#"}{fieldName}
        </if>

    </delete>
    
</mapper>